/**
 * 请求，拦截器，文件上传等
 * @author wenqiang
 **/
import Request, { ConfigType } from './request'
import { interfaceUrl, showLoading, getToken, toast } from './utils'

const request = new Request()

//   /**
//    * 请求拦截器
//    * @param string url 请求地址
//    * @param string method 请求方式
//    *  GET or POST
//    * @param {*} postData 请求参数
//    * @param bool isDelay 是否延迟显示loading
//    * @param bool isForm 数据格式
//    *  true: 'application/x-www-form-urlencoded'
//    *  false:'application/json'
//    * @param bool hideLoading 是否隐藏loading
//    *  true: 隐藏
//    *  false:显示
//    */
let delayed: any = null
let loading = false
let hideLoading = false
request.interceptors.request((request: any) => {
  loading && uni.hideLoading();
  loading = false;
  hideLoading = request.hideLoading
  if (!hideLoading) {
    if (request.isDelay) {
      delayed = setTimeout(() => {
        loading = true
        showLoading()
      }, 1000)
    } else {
      loading = true
      showLoading()
    }
  }
  request.header = {
    'content-type': request.isForm ? 'application/x-www-form-urlencoded' : 'application/json',
    'Authorization': getToken()
  }
  return request
})

// 响应拦截器
request.interceptors.response((response: any) => {
  console.log(response)
  if (response && response.code === 200) {
    clearTimeout(delayed as number)
    delayed = null;
    if (loading && !hideLoading) {
      uni.hideLoading()
    }

  } else {
    clearTimeout(delayed)
    delayed = null;
    toast(response.errMsg || "网络不给力，请稍后再试~")
  }

  return response
})

// 设置默认配置
request.setConfig((config: ConfigType) => {
  console.log(import.meta.env)

  config.baseURL = 'https://www.baidu.com'
  // 判断环境
  if (import.meta.env.MODE === "development") {
    config.baseURL = 'https://www.baidu.com'
  }
  return config
})

/**
 * get请求
 * @param string url 请求地址
 * @param string data 请求参数
 * @param object options 接口配置
 */
export const get = (url: string, data?: any, options: ConfigType = {}) => {
  return request.get(url, data, options)
}
/**
 * post请求
 * @param string url 请求地址
 * @param string data 请求参数
 * @param object options 接口配置
 */
export const post = (url: string, data?: any, options: ConfigType = {}) => {
  return request.post(url, data, options)
}

/**
 * 上传文件
 * @param string url 请求地址
 * @param string src 文件路径
 */
export const uploadFile = function (url: string, src: string) {
  showLoading()
  return new Promise((resolve, reject) => {
    const uploadTask = uni.uploadFile({
      url: interfaceUrl() + url,
      filePath: src,
      name: 'imageFile',
      header: {
        'Authorization': getToken()
      },
      formData: {
        // sizeArrayText:""
      },
      success: function (res: any) {
        uni.hideLoading()
        let d = JSON.parse(res.data.replace(/\ufeff/g, "") || "{}")
        if (d.code % 100 == 0) {
          //返回图片地址
          let fileObj = d.data;
          resolve(fileObj)
        } else {
          toast(res.msg);
        }
      },
      fail: function (res: any) {
        reject(res)
        toast(res.msg);
      }
    })
  })
}

export const http = {
  get,
  post,
  uploadFile
}

